package com.project.movie.controllers;



import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.project.movie.dao.ActorRepository;
import com.project.movie.dao.DirectorRepository;
import com.project.movie.dao.MovieRepository;
import com.project.movie.dao.RoleRepository;
import com.project.movie.dto.ActorMovie;
import com.project.movie.dto.ActorRole;
import com.project.movie.dto.DirectorMovie;
import com.project.movie.dto.ScoreData;
import com.project.movie.entities.Movies;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.List;

@Controller
public class HomeControllers {

    @Autowired
    MovieRepository movieRepository;
    @Autowired
    RoleRepository roleRepository;
    @Autowired
    DirectorRepository directorRepository;
    @Autowired
    ActorRepository actorRepository;


    @GetMapping("/")
    public String displayHome(Model model) throws JsonProcessingException {

         List<Movies> movies = movieRepository.findAll();
         model.addAttribute("movies",movies);

         List<ScoreData> scoreData = movieRepository.getMovieScore();
        ObjectMapper objectMapper = new ObjectMapper();

       String jsonString = objectMapper.writeValueAsString(scoreData);
       model.addAttribute("scoreData",jsonString);

        List<ActorMovie> actorMovies = actorRepository.actorMovie();
        model.addAttribute("actorsMovie",actorMovies);

        List<ActorRole> actorRole = roleRepository.actorRole();
        model.addAttribute("roles",actorRole);

        List<DirectorMovie> directorMovies= directorRepository.directorMovies();
        model.addAttribute("directorMovie",directorMovies);

        return "home";
    }

}
